package com.MD5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class method1 {
    /**
     * 加盐方式
     * 生成32位md5码
     */
    public static String md5Password (String password){

        //获取信息摘要器对象
        try{
            //算法名
            MessageDigest digest=MessageDigest.getInstance("md5");
            byte[] result = digest.digest(password.getBytes());
            StringBuffer stringBuffer=new StringBuffer();
            for (byte b : result) {
                //与运算 取十六进制的后两位
                int number = b&0xff;//加盐
                String str=Integer.toHexString(number);
                //判断转化的是否位两位，不是则补个零
                if (str.length()==1){
                    stringBuffer.append("0");
                }
                stringBuffer.append(str);
            }
            return stringBuffer.toString();
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }
    }
    public static void main(String[] args) throws NoSuchAlgorithmException {
//        MessageDigest digest=MessageDigest.getInstance("md5");
//        byte[] result = digest.digest("password".getBytes());
//        System.out.println(Arrays.toString(result));
//
//        System.out.println(Arrays.toString("password".getBytes()));
        //dd中对应的是每个字母的十进制形式。
//        byte[] dd= "password".getBytes();
//        System.out.println(Arrays.toString(dd));
        String s = md5Password("274374634");
        System.out.println(s.equals("fa8fc1b41cc67308aabf36048505d582"));


    }
}
